home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / CommentBlock.java < prev    next >
Text File  |  1998-10-07  |  16KB  |  502 lines

  1. import java.lang.*;
  2. import java.awt.*;
  3. import java.io.*;
  4. import java.util.*;
  5.  
  6. import com.sun.java.swing.*;
  7. import com.sun.java.swing.JLabel;
  8.  
  9. import com.symantec.itools.vcafe.macro.VisualCafeCommand;
  10. import com.symantec.itools.vcafe.openapi.VisualCafe;
  11. import com.symantec.itools.vcafe.openapi.*;
  12.  
  13. public class CommentBlock extends java.lang.Object
  14. {
  15.     public static String C_STYLE = "C_STYLE";
  16.     public static String CPP_STYLE = "CPP_STYLE";
  17.     public static String ASK_USER = "ASK_USER";
  18.     public static String NONE = "NONE";
  19.     
  20.     private CommentDialog cpiAsk;
  21.     
  22.     public static StringBuffer _sbModifiedBlock;  //static to make Undo work
  23.  
  24.     public CommentBlock() {}
  25.  
  26.     private class CommentDialog extends com.symantec.itools.vcafe.openapi.pluginapi.PluginDialog
  27.     {
  28.         private boolean _bOK;     //check to see if OK button was clicked
  29.         
  30.         public CommentDialog(String title, boolean modal)
  31.         {
  32.             super(title, modal);
  33.  
  34.             //_myPlugIn = cpiPlugIn;
  35.             _bOK = false;
  36.             
  37.             //{{INIT_CONTROLS
  38.             setLayout(null);
  39.             setFont(new Font("Dialog", Font.PLAIN, 10));
  40.             setSize(266,242);
  41.             lbl_choose.setText("Choose a comment style");
  42.             add(lbl_choose);
  43.             lbl_choose.setFont(new Font("Dialog", Font.BOLD, 12));
  44.             lbl_choose.setBounds(55,10,155,25);
  45.             bgp_style.setAlignmentX(0.0F);
  46.             bgp_style.setLayout(new BoxLayout(bgp_style,BoxLayout.Y_AXIS));
  47.             add(bgp_style);
  48.             bgp_style.setBounds(47,40,185,45);
  49.             rb_c_style.setText("C Style    /*  */");
  50.             rb_c_style.setActionCommand("C Style    /*  */");
  51.             rb_c_style.setSelected(true);
  52.             bgp_style.add(rb_c_style);
  53.             rb_c_style.setBounds(0,0,100,25);
  54.             rb_cpp_style.setText("C++ Style   //");
  55.             rb_cpp_style.setActionCommand("C++ Style   //");
  56.             bgp_style.add(rb_cpp_style);
  57.             rb_cpp_style.setBounds(0,25,95,25);
  58.             btn_ok.setText("OK");
  59.             btn_ok.setActionCommand("OK");
  60.             btn_ok.setOpaque(false);
  61.             add(btn_ok);
  62.             btn_ok.setBounds(50,95,85,20);
  63.             add(cb_no_show_again);
  64.             cb_no_show_again.setBounds(30,160,15,20);
  65.             tp_no_show_again.setText("Always use the selected style and do not show this dialog again.  See vcafe.macro.properties to manually change this dialog's settings.");
  66.             add(tp_no_show_again);
  67.             tp_no_show_again.setBounds(50,125,185,105);
  68.             tp_no_show_again.setSelectionEnd(135);
  69.             tp_no_show_again.setSelectionStart(135);
  70.             tp_no_show_again.setCaretPosition(135);
  71.             btn_cancel.setText("Cancel");
  72.             btn_cancel.setActionCommand("OK");
  73.             btn_cancel.setOpaque(false);
  74.             add(btn_cancel);
  75.             btn_cancel.setBounds(150,95,85,20);
  76.             //}}
  77.  
  78.             //{{REGISTER_LISTENERS
  79.             SymAction lSymAction = new SymAction();
  80.             btn_ok.addActionListener(lSymAction);
  81.             btn_cancel.addActionListener(lSymAction);
  82.             //}}
  83.  
  84.             rb_c_style.setSelected(true);
  85.  
  86.         }
  87.  
  88.  
  89.         //{{DECLARE_CONTROLS
  90.         com.sun.java.swing.JLabel lbl_choose = new com.sun.java.swing.JLabel();
  91.         com.symantec.itools.swing.JButtonGroupPanel bgp_style = new com.symantec.itools.swing.JButtonGroupPanel();
  92.         com.sun.java.swing.JRadioButton rb_c_style = new com.sun.java.swing.JRadioButton();
  93.         com.sun.java.swing.JRadioButton rb_cpp_style = new com.sun.java.swing.JRadioButton();
  94.         com.sun.java.swing.JButton btn_ok = new com.sun.java.swing.JButton();
  95.         com.sun.java.swing.JCheckBox cb_no_show_again = new com.sun.java.swing.JCheckBox();
  96.         com.sun.java.swing.JTextPane tp_no_show_again = new com.sun.java.swing.JTextPane();
  97.         com.sun.java.swing.JButton btn_cancel = new com.sun.java.swing.JButton();
  98.         //}}
  99.  
  100.         class SymAction implements java.awt.event.ActionListener
  101.         {
  102.             public void actionPerformed(java.awt.event.ActionEvent event)
  103.             {
  104.                 Object object = event.getSource();
  105.                 if (object == btn_ok)
  106.                     jButton1_actionPerformed(event);
  107.                 else if (object == btn_cancel)
  108.                     btnCancel_actionPerformed(event);
  109.             }
  110.         }
  111.  
  112.         void jButton1_actionPerformed(java.awt.event.ActionEvent event)
  113.         {
  114.             // to do: code goes here.
  115.             _bOK = true;     
  116.             jButton1_actionPerformed_interaction_1(event);
  117.         }
  118.  
  119.  
  120.         void jButton1_actionPerformed_interaction_1(java.awt.event.ActionEvent event)
  121.         {
  122.             // CommentDialog Hide the CommentDialog
  123.             //_myPlugIn.close();
  124.             doCommentBlock();
  125.             this.setVisible(false);
  126.             this.dispose();
  127.             
  128.         }
  129.         
  130.         public boolean isSaveStyle()
  131.         {
  132.             return cb_no_show_again.isSelected();
  133.         }
  134.  
  135.         void btnCancel_actionPerformed(java.awt.event.ActionEvent event)
  136.         {
  137.             // to do: code goes here.
  138.             _bOK = false;
  139.             //_myPlugIn.close();
  140.             doCommentBlock();
  141.             this.setVisible(false);
  142.             this.dispose();
  143.  
  144.         }
  145.  
  146.         public String getStyle()
  147.         {
  148.             if(!_bOK) return "NONE";
  149.             if(rb_c_style.isSelected()) return "C_STYLE";
  150.             else return "CPP_STYLE";
  151.             
  152.         }
  153.         
  154.  
  155.     }
  156.  
  157.     //******
  158.     // End CommentDialog
  159.     //************************************************
  160.  
  161.     com.symantec.itools.vcafe.openapi.VisualCafe _vcMainCafe;
  162.     com.symantec.itools.vcafe.openapi.SourceFile _sfSource;
  163.  
  164.     /*  
  165.      * public void run() is the entry point for macros
  166.      */
  167.     public void run()
  168.     {
  169.         com.symantec.itools.vcafe.openapi.Range rSelectedBlock;
  170.         Object oFrontMostWin;
  171.         String sCommentStyle;
  172.         Properties pCommentProp;
  173.  
  174.  
  175.         // 1. Get the open source window
  176.         _vcMainCafe = VisualCafe.getVisualCafe();
  177.         oFrontMostWin = _vcMainCafe.getFrontmostWindow();
  178.         if(! (oFrontMostWin instanceof SourceFile))
  179.         {
  180.             return;  // A source window doesn't have focus
  181.         }
  182.         else 
  183.         {
  184.             _sfSource = (SourceFile)oFrontMostWin;
  185.         }
  186.         
  187.         // 2. Get properties to find out comment style
  188.         sCommentStyle = getCommentStyle();
  189.  
  190.         // 3. Do appropriate sytle
  191.         putComments(sCommentStyle);
  192.     }
  193.  
  194.     /**
  195.      * Insert comment tags based on sStyle specified
  196.      */
  197.     private void putComments(String sStyle)
  198.     {
  199.         if(sStyle.equals(NONE)) return;
  200.         else
  201.         if(sStyle.equals(ASK_USER)) displayCommentStyleDialog();
  202.         else
  203.         if(sStyle.equals(C_STYLE)) putCStyle(_sfSource);
  204.         else
  205.         if(sStyle.equals(CPP_STYLE)) putCPPStyle(_sfSource);
  206.     }
  207.  
  208.     /**
  209.      * Insert C-style comment tags in the selected block
  210.      */
  211.     public void putCStyle(SourceFile sfCurrentSource)
  212.     {
  213.         com.symantec.itools.vcafe.openapi.Range rSelectedBlock;
  214.         StringBuffer sbBlock;
  215.         String sCommentStart = "/*";
  216.         String sCommentEnd = "*/";
  217.  
  218.         // Get the selected block to comment
  219.         rSelectedBlock = sfCurrentSource.getSelectionRange();
  220.  
  221.         // Put in C-Style comment tags
  222.         sbBlock = sfCurrentSource.getRangeText(rSelectedBlock);
  223.  
  224.         if (sbBlock.length() <= 0)
  225.             return;
  226.  
  227.         String sTrimBlock = sbBlock.toString();
  228.         
  229.         if(sTrimBlock.charAt(sTrimBlock.length()-1) == '\n')
  230.         {
  231.             sbBlock = new StringBuffer(sTrimBlock.substring(0, sTrimBlock.length()-1));
  232.             sCommentEnd = "*/\n";
  233.         }
  234.  
  235.         // Paste in new C-Style block with comment tags
  236.         sbBlock.insert(0, sCommentStart);
  237.         sbBlock.append(sCommentEnd);
  238.         _sbModifiedBlock = sbBlock;
  239.         sfCurrentSource.setRangeText(_sbModifiedBlock, rSelectedBlock);
  240.         VisualCafeCommand.textEndOfLine();
  241.     }
  242.  
  243.     /**
  244.      * Insert C++ style comment tags in the selected block
  245.      */
  246.     public void putCPPStyle(SourceFile sfCurrentSource)
  247.     {
  248.         com.symantec.itools.vcafe.openapi.Range rSelectedBlock;
  249.         StringBuffer sbBlock;
  250.         String sCommentStart = "//";
  251.         int iTotalLen;
  252.         int iCurPos = 0;
  253.  
  254.         // Get the selected block to comment
  255.         rSelectedBlock = sfCurrentSource.getSelectionRange();
  256.  
  257.         // Put in CPP-Style comment tags
  258.         sbBlock = sfCurrentSource.getRangeText(rSelectedBlock);
  259.  
  260.         // Make sure text is selected
  261.         sbBlock = sfCurrentSource.getRangeText(rSelectedBlock);
  262.  
  263.         iTotalLen = sbBlock.length();
  264.  
  265.         if (iTotalLen <= 0)
  266.             return;
  267.  
  268.         if(iCurPos < iTotalLen)
  269.         {
  270.             sbBlock.insert(iCurPos, sCommentStart);
  271.             iTotalLen+=2;
  272.             iCurPos+=2;
  273.         }
  274.  
  275.         // Put in rest of comment marks
  276.         while(iCurPos < iTotalLen)
  277.         {
  278.             if(sbBlock.charAt(iCurPos) == '\n')
  279.             {   
  280.                 if(iCurPos+1 < iTotalLen)
  281.                 {
  282.                     sbBlock.insert(iCurPos+1, sCommentStart);
  283.                     iTotalLen+=2;
  284.                     iCurPos+=1;
  285.                 }
  286.                 
  287.             }
  288.             iCurPos++;
  289.         }
  290.         
  291.         // Paste in new CPP-Style block with comment tags
  292.         _sbModifiedBlock = sbBlock;
  293.         sfCurrentSource.setRangeText(_sbModifiedBlock, rSelectedBlock);
  294.         VisualCafeCommand.textEndOfLine();
  295.     }
  296.  
  297.     /**
  298.      *  Reads from <vcafe>\bin\macs\src\vcafe.macro.properties to get the
  299.      *  user's preferred comment style.  If the file isn't there, it will
  300.      *  be created.
  301.      */
  302.     private String getCommentStyle()
  303.     {
  304.         Properties pCommentProp;
  305.         boolean bAskStyle;
  306.         String sMacroSrc = "bin/macs/src";
  307.         String sPropFile = "vcafe.macro.properties";
  308.         String sCommentKey = "comment.style";
  309.         String sVCafeLoc = VisualCafe.getVisualCafe().getDirectory();
  310.         String sFullPropPath = sVCafeLoc+"/"+sPropFile;
  311.         File fPropFile = null;
  312.         FileInputStream fisPropFile = null;
  313.         String sStyle = ASK_USER;
  314.  
  315.         try {
  316.             fPropFile = new File(sFullPropPath);
  317.             if(!fPropFile.exists()) createPropFile(sFullPropPath);
  318.  
  319.             pCommentProp = new Properties();
  320.             fisPropFile = new FileInputStream(fPropFile);
  321.             pCommentProp.load(fisPropFile);
  322.             sStyle = pCommentProp.getProperty("comment.style");
  323.  
  324.             // If comment.style wasn't found, add to propfile and try again
  325.             if( (sStyle == null) || sStyle.equals("") )
  326.             {
  327.                 createPropFile(sFullPropPath);
  328.                 pCommentProp.load(fisPropFile);
  329.                 sStyle = pCommentProp.getProperty(sCommentKey);
  330.             }
  331.  
  332.         }
  333.         catch(FileNotFoundException fnfe)
  334.         {
  335.             sStyle = ASK_USER;
  336.             fnfe.printStackTrace();
  337.         }
  338.         catch(IOException ioe)
  339.         {
  340.             ioe.printStackTrace();
  341.         }
  342.         finally
  343.         {
  344.             try
  345.             {
  346.                 fisPropFile.close();
  347.             }
  348.             catch (IOException ioe)
  349.             {
  350.                 System.out.println("Couldn't close vcafe.macro.properties");
  351.             }
  352.  
  353.         }
  354.         //return  sStyle;
  355.         return sStyle.trim();
  356.     }
  357.  
  358.     /**
  359.      * Display dialog that prompts user for comment style.
  360.      */
  361.     private void displayCommentStyleDialog()
  362.     {
  363.         cpiAsk = new CommentDialog("Comment Block", true);
  364.                
  365.         //* centerDialogInVcafe()
  366.         {
  367.             Rectangle bounds;
  368.             if (VisualCafe.getVisualCafe().isMDIEnabled())
  369.                 bounds = VisualCafe.getVisualCafe().getMainFrameBounds();
  370.             else
  371.             {
  372.                 Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  373.                 bounds = new Rectangle(d);
  374.             }
  375.                     
  376.             Rectangle abounds = cpiAsk.getBounds();
  377.             cpiAsk.setLocation(bounds.x + (bounds.width - abounds.width)/ 2,
  378.                         bounds.y + (bounds.height - abounds.height)/2);
  379.         }
  380.         
  381.         cpiAsk.addWindowListener(new CommentDialogListener());
  382.         cpiAsk.setVisible(true);
  383.     }
  384.  
  385.     /**
  386.      *  This property file is located in <vcafe>\bin\macs\src
  387.      */
  388.     private void createPropFile(String sPropFileName)
  389.     {
  390.         FileWriter fwNewPropFile;
  391.         PrintWriter pwNewPropFile;
  392.  
  393.         try
  394.         {
  395.             fwNewPropFile = new FileWriter(sPropFileName, true);
  396.             pwNewPropFile = new PrintWriter(fwNewPropFile);
  397.  
  398.             pwNewPropFile.println("# Comment Macro Properties");
  399.             pwNewPropFile.println("# Styles:");
  400.             pwNewPropFile.println("#    C_STYLE - /*    */");
  401.             pwNewPropFile.println("#    CPP_STYLE - //");
  402.             pwNewPropFile.println("#    ASK_USER - Prompt with dialog asking for C or CPP style");
  403.             pwNewPropFile.println("");
  404.             pwNewPropFile.println("comment.style=ASK_USER");
  405.  
  406.             fwNewPropFile.close();
  407.         }
  408.         catch(IOException ioe)
  409.         {
  410.             ioe.printStackTrace();
  411.         }
  412.     }
  413.  
  414.     /**
  415.      *  Save the comment style to file
  416.      */
  417.     private String saveCommentStyle( String sStyle)
  418.     {
  419.         String sMacroSrc = "bin/macs/src";
  420.         String sPropFile = "vcafe.macro.properties";
  421.         String sCommentKey = "comment.style";
  422.         String sVCafeLoc = VisualCafe.getVisualCafe().getDirectory();
  423.         String sFullPropPath = sVCafeLoc+"/"+sPropFile;
  424.         File fPropFile = null;
  425.  
  426.         if(sStyle.equals(NONE)) return NONE;
  427.         try {
  428.             fPropFile = new File(sFullPropPath);
  429.             if(!fPropFile.exists()) createPropFile(sFullPropPath);
  430.  
  431.             String sCurrentLine;
  432.             boolean bFoundIt;
  433.             long lLineBegin = 0;
  434.             long lLineEnd = 0;
  435.             byte[] baLineRead;
  436.  
  437.             RandomAccessFile rafProp = new RandomAccessFile(fPropFile, "rw");
  438.  
  439.             bFoundIt = false;
  440.             sCurrentLine = " ";
  441.  
  442.             while( (sCurrentLine != null) && !bFoundIt)
  443.             {
  444.                 lLineBegin = rafProp.getFilePointer();
  445.                 sCurrentLine = rafProp.readLine();
  446.                 lLineEnd = rafProp.getFilePointer();
  447.  
  448.                 if(sCurrentLine.startsWith(sCommentKey))
  449.                 {
  450.                     String sReplace = sCommentKey+"="+sStyle+"            \n";
  451.                     rafProp.seek(lLineBegin);
  452.                     rafProp.writeBytes(sReplace);
  453.                     bFoundIt = true;
  454.                 }
  455.             }
  456.  
  457.             if(!bFoundIt)
  458.             {
  459.                     String sReplace = sCommentKey+"="+sStyle+"            \n";
  460.                     rafProp.seek(lLineBegin);
  461.                     rafProp.writeBytes(sReplace);
  462.             }
  463.             if(rafProp != null) rafProp.close();
  464.             
  465.         }
  466.         catch(FileNotFoundException fnfe)
  467.         {
  468.             sStyle = ASK_USER;
  469.             fnfe.printStackTrace();
  470.         }
  471.         catch(IOException ioe)
  472.         {
  473.             ioe.printStackTrace();
  474.         }
  475.         finally
  476.         {}
  477.  
  478.         return sStyle.trim();
  479.     }
  480.  
  481.     /**
  482.      *  does the user input instructions from the Comment Dialog
  483.      */
  484.     private void doCommentBlock()
  485.     {
  486.         String sStyle = cpiAsk.getStyle();
  487.         if (cpiAsk.isSaveStyle())
  488.             saveCommentStyle(sStyle);
  489.                 
  490.         putComments(sStyle);
  491.     }
  492.  
  493.     class CommentDialogListener extends java.awt.event.WindowAdapter
  494.     {
  495.         public void windowClosing(java.awt.event.WindowEvent event)
  496.         {
  497.             cpiAsk.setVisible(false);
  498.             cpiAsk.dispose();
  499.         }
  500.     }
  501.     
  502. }